//
//  SZZNetworkTools.swift
//  SZZweibo
//
//  Created by 申壮壮 on 16/8/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

import AFNetworking
// 网络请求枚举
enum SZZNetworkToolsMethod: String {
    // GET 请求
    case GET = "GET"
    // POST 请求
    case POST = "POST"
}

class SZZNetworkTools: AFHTTPSessionManager {
    static let sharedTools:SZZNetworkTools = {
        let tools = SZZNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
        func request(method: SZZNetworkToolsMethod, urlString: String, parameters: AnyObject?, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        
        if method == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                
                success(response: response)
                }, failure: { (_, error) -> Void in
                    
                    failure(error: error)
            })
            
        }else {
            
            POST(urlString, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                
                success(response: response)
                }, failure: { (_, error) -> Void in
                    
                    failure(error: error)
            })
            
        }
        
    }
    
}

extension SZZNetworkTools{
    func homeLoadData(success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求的urlString
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params = ["access_token": SZZUserAccountViewModel.sharedTools.userAccount!.access_token!]
        request(SZZNetworkToolsMethod.GET, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }
}

// - 授权界面接口
extension SZZNetworkTools {
    // 请求授权
    func oauthLoadUserAccount(code: String, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求urlString
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id":WBAppKey,
            "client_secret": WBAppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri": WBRedirect_uri
        ]
        
        request(SZZNetworkToolsMethod.POST, urlString: urlString, parameters: params, success: success, failure: failure)
    }
    
    // 请求个人信息
    func oauthLoadUserInfo(userAccount: SZZUserAcconuntModel, success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        // 请求urlString
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]
        
        request(SZZNetworkToolsMethod.GET, urlString: urlString, parameters: params, success: success, failure: failure)
    }
}

// - 请求天气接口
extension SZZNetworkTools {
    func getWeatherData(success:(response: AnyObject?)->(), failure:(error: NSError)->()){
        let urlString = "http://www.weather.com.cn/data/sk/101010100.html"
        request(SZZNetworkToolsMethod.GET, urlString: urlString, parameters: nil, success: success, failure: failure)
    }
}

